import os
import glob
Import('env')
Import('conf')

# Sources
sources = []
patterns = Split('src/*.cs \
                  src/*/*.cs \
                  ../AssemblyInfo.cs')

for pattern in patterns:
	for source in glob.glob(pattern):
		sources.append(source)

# Resources
resources = []
patterns = Split('resources/*.png \
                  resources/*.ui \
                  resources/glade/*.glade')

for pattern in patterns:
	for resource in glob.glob(pattern):
		resources.append('#gtk/' + resource)

# Configure

cscflags = Split('-define:ENABLE_DBUS,ENABLE_NOTIFICATIONS')

nativeDBus = nativeNotify = False

if not env.GetOption('clean'):
	if not conf.CheckPKG('gtk-sharp-2.0 >= 2.6'):
		print 'gtk-sharp-2.0 >= 2.6 not found.'
		Exit(1)
	
	gtk_2_10 = conf.CheckPKG('gtk-sharp-2.0 >= 2.10')
	
	if not conf.CheckPKG('glade-sharp-2.0 >= 2.6'):
		print 'glade-sharp-2.0 >= 2.6 not found.'
		Exit(1)
	
	gnome = env['GNOME']
	if gnome and not conf.CheckPKG('gnome-sharp-2.0 >= 2.6'):
		print 'gnome-sharp-2.0 >= 2.6 not found.'
		print 'Build with GNOME=false if you want to disable the GNOME integration.'
		Exit(1)
	
	if gtk_2_10: cscflags.append('-define:GTK_2_10')
	if gnome: cscflags.append('-define:GNOME')
	
	nativeDBus = conf.CheckPKG('ndesk-dbus-1.0 >= 0.4') and conf.CheckPKG('ndesk-dbus-glib-1.0 >= 0.3')
	nativeNotify = conf.CheckPKG('notify-sharp >= 0.4')
	
	print
	print ' * Using system ndesk-dbus:', nativeDBus and 'yes' or 'no'
	print ' * Using system notify-sharp:', nativeNotify and 'yes' or 'no'
	print ' * Using Gtk+ 2.10 widgets:', gtk_2_10 and 'yes' or 'no'
	print ' * GNOME integration:', gnome and 'yes' or 'no'
	print

# Build

dbusEnv = env.Copy();
if nativeDBus:
	dbusEnv.Append(PKG = Split('ndesk-dbus-1.0 \
	                            ndesk-dbus-glib-1.0'))
else:
	dbusEnv.Append(REF = Split('#lib/NDesk.DBus.dll \
	                            #lib/NDesk.DBus.GLib.dll'))

localEnv = dbusEnv.Copy()

if not nativeNotify:
	Export('dbusEnv')
	SConscript('#lib/notify-sharp/SConscript')
	
	localEnv.Append(REF = '#lib/notify-sharp/notify-sharp.dll')
else:
	localEnv.Append(PKG = 'notify-sharp')

localEnv.Append(REF = '#bin/DCSharp.Base.dll',
                GREF = Split('Mono.Posix.dll \
                              Mono.GetOptions.dll'),
                PKG = Split('gtk-sharp-2.0 \
                             glade-sharp-2.0 \
                             gnome-sharp-2.0'),
                CSCFLAGS = cscflags,
                RESOURCE = resources)

localEnv.CLIProgram('#bin/DCSharp.exe', sources)


# Install

lib_dir = env['DESTDIR'] + os.sep + os.path.join(env['LIBDIR'], 'dcsharp')
schemas_dir = env['DESTDIR'] + os.sep + os.path.join(env['SYSCONFDIR'], 'gconf', 'schemas')

env.Alias("install", env.InstallData(schemas_dir, 'dcsharp-handlers.schema'))

env.Alias("install", env.InstallProgram(lib_dir, '#bin/DCSharp.exe'))
env.Alias("install", env.InstallData(lib_dir, '#bin/DCSharp.exe.config'))
env.Alias("install", env.InstallLibrary(lib_dir, '#lib/NDesk.DBus.dll'))
env.Alias("install", env.InstallLibrary(lib_dir, '#lib/NDesk.DBus.GLib.dll'))
env.Alias("install", env.InstallData(lib_dir, '#lib/NDesk.DBus.GLib.dll.config'))

SConscript('po/SConscript')
